package com.ostrich.cloud.ostrichDistributionWeb.app.controller;

import ch.qos.logback.core.util.TimeUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichDistributionService.client.DeliveryOrderClient;
import com.ostrich.cloud.ostrichDistributionService.client.param.FinishedParam;
import com.ostrich.cloud.ostrichDistributionService.client.param.PageSelectForm;
import com.ostrich.cloud.ostrichDistributionWeb.client.RiderClient;
import com.ostrich.cloud.ostrichDistributionWeb.client.param.UpdateForm;
import com.ostrich.cloud.ostrichUserService.client.RiderMangerClient;
import com.ostrich.ostrichUserService.dto.RiderDto;
import com.ostrich.ostrichUserService.param.LoginCusParam;
import com.ostrich.ostrichUserService.param.rider.UpdateRider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@Slf4j
public class RiderWebController implements RiderClient {
    @Resource
    private RiderMangerClient riderMangerClient;
    @Resource
    private DeliveryOrderClient deliveryOrderClient;


    @Override
    public Result login(@RequestParam("name") String name,@RequestParam("password") String pwd) {
        LoginCusParam form=new LoginCusParam();
        form.setUsername(name);
        form.setPassword(pwd);
        Result result = riderMangerClient.riderLogin(form);
        return result;
    }

    @Override
    public Result getInfo(@RequestParam("riderId") Integer rid) {
        Result result = riderMangerClient.getRider(rid);
        return result;
    }

    @Override
    public Result reInfo(@RequestBody UpdateForm updateForm) {
        UpdateRider form=new UpdateRider();
        form.setBankNumber(updateForm.getBankNumber());
        form.setIdentityNumber(updateForm.getIdentityNumber());
        form.setPhoneNumber(updateForm.getPhoneNumber());
        Result result = riderMangerClient.updateRider(form);
        return result;
    }

    @Override
    public Result getbanlance() {
        Result result = riderMangerClient.getRiderById();
        RiderDto rider = (RiderDto)result.getData();
        BigDecimal riderMoney = rider.getRiderMoney();
        if(rider==null){
            log.info("没有查询到骑手");
            return Result.error();
        }
        return Result.ok(riderMoney);
    }

    @Override
    public Result toWay(@RequestParam("riderId") Integer rid) {
        FinishedParam form=new FinishedParam();
        form.setDeliveryId(rid);
        form.setFinishTime(DateUtil.date());
        Result result = deliveryOrderClient.finishedDelicery(form);
        return result;
    }

    @Override
    public Result getDeliveryOrder(PageSelectForm pageSelectForm) {


        Result deliveryPage = deliveryOrderClient.getDeliveryPage(pageSelectForm);
        return  deliveryPage;

    }
}
